from sqlalchemy import Column, String, DECIMAL, Boolean, DateTime
from sqlalchemy.dialects.mysql import TINYINT
from pydantic import BaseModel
from server.model import BaseOrm
from datetime import datetime
from server import db


class GymStatus:
    Serv = 0
    Not = 1


class GymType:
    Badminton = 0
    AirVolleyball = 1
    BasketBall = 2


class GymCreate(BaseModel):
    name: str
    address: str


class GymSchema(GymCreate):
    id: int

    class Config:
        orm_mode = True


# 球馆
class Gym(BaseOrm):
    __tablename__ = 'gym'
    name = Column(String(64), nullable=False, server_default="", comment="名称")
    gym_type = Column(TINYINT(unsigned=True), nullable=False, server_default="0", comment="状态: 0:羽毛球馆 1:气排球馆 2:篮球馆")
    address = Column(String(40), nullable=False, server_default="", index=True, comment="地址")
    longitude = Column(DECIMAL(10, 6), comment="经度")
    latitude = Column(DECIMAL(10, 6), comment="纬度")
    status = Column(TINYINT(unsigned=True), nullable=False, server_default="0", comment="状态: 0:营业 1:未营业")
    phone = Column(String(40), nullable=False, server_default="", comment="联系电话")
    desc = Column(String(1023), nullable=False, server_default="", comment="场地介绍")
    services = Column(String(255), nullable=False, server_default="", comment="场地服务")
    judgment = Column(Boolean, nullable=False, server_default="0", comment="裁判服务: 0:否 1:是")
    tips = Column(String(255), nullable=False, server_default="", comment="标签")
    open_time = Column(String(16), nullable=False, server_default="00:00:00", comment="开门时间")
    close_time = Column(String(16), nullable=False, server_default="24:00:00", comment="关门时间")
    day_price = Column(DECIMAL(6, 2), nullable=False, server_default="0", comment="白天单价")
    night_price = Column(DECIMAL(6, 2), nullable=False, server_default="0", comment="夜晚单价")
    we_day_price = Column(DECIMAL(6, 2), nullable=False, server_default="0", comment="周末白天单价")
    we_night_price = Column(DECIMAL(6, 2), nullable=False, server_default="0", comment="周末夜晚单价")

    def count_price(self, hour, date_str):
        day_price = self.day_price
        night_price = self.night_price
        if datetime.strptime(date_str, "%Y-%m-%d").weekday() + 1 in {6, 7}:
            day_price = self.we_day_price
            night_price = self.we_night_price
        return day_price if hour < 17 else night_price


db.create_all()
